﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;
using HYCodeBase.XmlUtility;

namespace HYCodeBase.WinForm.WinFormRemindSuit
{
	public class WinFormRemindManger
	{
		public WinFormRemindManger(System.Windows.Forms.Form inputForm, string inputKeyString = null)
		{
			this.m_useTransformerFactory = new ControlTypeMangerFactory();
			this.m_thisForm = inputForm;
			this.m_strKeyString = inputKeyString;
		}

		public WinFormRemindManger(System.Windows.Forms.Form inputFrom, List<KeyValuePair<ControlPacker, bool>> inputlstControlNeeadRemind, string inputKeyString = null)
		{
			this.m_useTransformerFactory = new ControlTypeMangerFactory();
			this.m_thisForm = inputFrom;
			this.m_lstControlNeedRemind = inputlstControlNeeadRemind;
			this.m_strKeyString = inputKeyString;
		}

		public void AddOneControl(ControlPacker inputControl, bool ifSpecialControl = false)
		{
			this.m_lstControlNeedRemind.Add(new KeyValuePair<ControlPacker, bool>(inputControl, ifSpecialControl));
		}

		public bool TrySaveFormToXmlNode(ref XmlDocument inputXmlDoc, out XmlNode creatXmlNode)
		{
			creatXmlNode = null;
			BaseControlTransFormer controlTransFormer = this.m_useTransformerFactory.GetControlTransFormer(new ControlPacker
			{
				UseControl = this.m_thisForm
			}, false);
			if (controlTransFormer == null)
			{
				return false;
			}
			this.m_useFormInformationBean = (controlTransFormer.GetControlInformationBean() as FormInformationBean);
			if (this.m_useFormInformationBean == null)
			{
				return false;
			}
			foreach (KeyValuePair<ControlPacker, bool> keyValuePair in this.m_lstControlNeedRemind)
			{
				controlTransFormer = this.m_useTransformerFactory.GetControlTransFormer(keyValuePair.Key, keyValuePair.Value);
				if (controlTransFormer != null)
				{
					ControlInformationBean controlInformationBean = controlTransFormer.GetControlInformationBean();
					if (controlInformationBean != null)
					{
						this.m_useFormInformationBean.AddOneControlBean(controlInformationBean);
					}
				}
			}
			XmlNodeInformationBean xmlNodeInformationBean = XmlNodeInformationTransformer.FormBeanToXmlNodeBean(this.m_useFormInformationBean);
			if (this.m_strKeyString != null)
			{
				xmlNodeInformationBean.StrInnerText = this.m_strKeyString;
			}
			creatXmlNode = xmlNodeInformationBean.ToXmlNode(ref inputXmlDoc);
			return creatXmlNode != null;
		}

		public bool TryRendXmlToForm(XmlNode inputXmlNode, out List<System.Windows.Forms.Control> lstNonSavedControl)
		{
			this.m_useFormInformationBean = null;
			bool flag = true;
			lstNonSavedControl = new List<System.Windows.Forms.Control>();
			List<System.Windows.Forms.Control> list = new List<System.Windows.Forms.Control>();
			list.Add(this.m_thisForm);
			foreach (KeyValuePair<ControlPacker, bool> keyValuePair in this.m_lstControlNeedRemind)
			{
				if (keyValuePair.Key != null)
				{
					list.Add(keyValuePair.Key.UseControl);
				}
			}
			XmlNodeInformationBean xmlNodeInformationBean = XmlNodeInformationBean.ReadXmlNode(inputXmlNode);
			if (xmlNodeInformationBean == null)
			{
				lstNonSavedControl = list;
				return false;
			}
			foreach (KeyValuePair<FormInformationBean, string> keyValuePair2 in XmlNodeInformationTransformer.ReadAllFormBeanFromXmlNodeBean(xmlNodeInformationBean))
			{
				if (keyValuePair2.Key.StrName.Equals(this.m_thisForm.Name))
				{
					if (!string.IsNullOrEmpty(this.m_strKeyString) && !string.IsNullOrEmpty(keyValuePair2.Value) && keyValuePair2.Value.Equals(this.m_strKeyString))
					{
						this.m_useFormInformationBean = keyValuePair2.Key;
						break;
					}
					if (this.m_strKeyString == null)
					{
						this.m_useFormInformationBean = keyValuePair2.Key;
						break;
					}
				}
			}
			if (this.m_useFormInformationBean == null)
			{
				lstNonSavedControl = list;
				return false;
			}
			BaseControlTransFormer controlTransFormer = this.m_useTransformerFactory.GetControlTransFormer(new ControlPacker
			{
				UseControl = this.m_thisForm
			}, false);
			if (controlTransFormer == null)
			{
				lstNonSavedControl = list;
				return false;
			}
			if (!controlTransFormer.SetControlInfromation(this.m_useFormInformationBean))
			{
				flag = (flag && false);
				lstNonSavedControl.Add(this.m_thisForm);
			}
			foreach (KeyValuePair<ControlPacker, bool> keyValuePair3 in this.m_lstControlNeedRemind)
			{
				ControlInformationBean controlBeanByName = this.m_useFormInformationBean.GetControlBeanByName(keyValuePair3.Key.UseControl.Name);
				if (controlBeanByName == null)
				{
					flag = (flag && false);
					lstNonSavedControl.Add(keyValuePair3.Key.UseControl);
				}
				else
				{
					controlTransFormer = this.m_useTransformerFactory.GetControlTransFormer(keyValuePair3.Key, keyValuePair3.Value);
					if (controlTransFormer == null)
					{
						flag = (flag && false);
						lstNonSavedControl.Add(keyValuePair3.Key.UseControl);
					}
					else if (!controlTransFormer.SetControlInfromation(controlBeanByName))
					{
						flag = (flag && false);
						lstNonSavedControl.Add(keyValuePair3.Key.UseControl);
					}
				}
			}
			return flag;
		}

		private System.Windows.Forms.Form m_thisForm;

		private List<KeyValuePair<ControlPacker, bool>> m_lstControlNeedRemind = new List<KeyValuePair<ControlPacker, bool>>();

		private IControlTransformerFactory m_useTransformerFactory;

		private FormInformationBean m_useFormInformationBean;

		private string m_strKeyString;
	}
}
